<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>验证二叉搜索树</title>
</head>
<body>
  <script>
var isValidBST = function (root) {
  let stack = [];
  
  let preVal = -Infinity;
  while (root || stack.length) {
    // 走到了二叉树最底最左边的一个节点，栈里面也存储了对应走过的路径
    while (root) {
      stack.push(root);
      root = root.left;
    }
    // 从栈中弹出
    root = stack.pop();
    console.log(stack);
    // 如果中序遍历得到的节点的值小于等于前一个 inorder，说明不是二叉搜索树
    if (root.val <= preVal) return false;
    preVal = root.val;
    // 把左 根 遍历好了，开始走右子树
    root = root.right;
  }
  return true;
};
console.log(isValidBST([5,1,4,null,null,3,6]));
  </script>
</body>
</html>